package com.sleepcamel.resteasyConfigurator.view.detail;

import org.eclipse.core.databinding.Binding;
import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.beans.BeansObservables;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.jface.databinding.swt.ISWTObservableValue;
import org.eclipse.jface.databinding.swt.SWTObservables;
import org.eclipse.jface.databinding.viewers.ViewersObservables;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ComboViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;

import com.sleepcamel.rest.domain.method.parameter.RESTMethodParameter;
import com.sleepcamel.rest.domain.method.parameter.RESTMethodParameterType;
import com.sleepcamel.rest.domain.method.parameter.RESTMethodParameterTypeName;

public class RESTMethodParameterView extends MethodParameterView<RESTMethodParameter> {

	private Text txtRestTypeName;
	private ComboViewer comboViewer;
	private Binding comboBinding;
	private Binding valueBinding;
	private Binding enabledBinding;
	private Button btnCheckButton;
	private Binding encodedBinding;
	
	public RESTMethodParameterView(Composite parent, int style) {
		super(parent, style);
	}

	@Override
	protected void createControl() {
		comboViewer = new ComboViewer(this, SWT.READ_ONLY);
 		comboViewer.setContentProvider(ArrayContentProvider.getInstance());
		comboViewer.setInput(RESTMethodParameterTypeName.values());
		
		txtRestTypeName = new Text(this, SWT.BORDER);
		txtRestTypeName.setText("");
		txtRestTypeName.setMessage("paramName");
		
		btnCheckButton = new Button(this, SWT.CHECK | SWT.VERTICAL | SWT.CENTER);
		btnCheckButton.setToolTipText("Encoded ?");
		super.createControl();
	}
	
	@Override
	protected void clearBindings() {
		super.clearBindings();
		clearBinding(comboBinding);
		clearBinding(valueBinding);
		clearBinding(enabledBinding);
		clearBinding(encodedBinding);
	}
	
	@Override
	protected DataBindingContext initDataBindings() {
		super.initDataBindings();
		
		RESTMethodParameterType restParameterType = modelData.getRestParameterType();
		
		IObservableValue widgetObservable = ViewersObservables.observeSingleSelection(comboViewer);
		comboBinding = m_bindingContext.bindValue(widgetObservable, BeansObservables.observeValue(restParameterType, "name"));
		
		if ( restParameterType.getName().isAcceptsValue() ){
			IObservableValue valueObserveWidget = SWTObservables.observeText(txtRestTypeName, SWT.Modify);
			IObservableValue valueObserveValue = BeansObservables.observeValue(restParameterType, "value");
			valueBinding = m_bindingContext.bindValue(valueObserveWidget, valueObserveValue, null, null);
		}
		ISWTObservableValue observeEnabled = SWTObservables.observeEnabled(txtRestTypeName);
		IObservableValue acceptsValue = BeansObservables.observeValue(restParameterType, "name.acceptsValue");
		enabledBinding = m_bindingContext.bindValue(observeEnabled, acceptsValue, null, null);
		
		IObservableValue encodedValue = SWTObservables.observeSelection(btnCheckButton);
		IObservableValue parameterEncodedObserveValue = BeansObservables.observeValue(modelData, "encodable");
		encodedBinding = m_bindingContext.bindValue(encodedValue, parameterEncodedObserveValue, null, null);
		
		return m_bindingContext;
	}

}
